<?php
require_once 'source/aop/AopClient.php';
require_once 'source/aop/request/AlipayFundTransToaccountTransferRequest.php';
class AlipayAop{
  private $appId; 
  private $rsaPrivateKey; 
  private $alipayrsaPublicKey; 
  function __construct(){ 
      $this->appId = '';
	  $this->rsaPrivateKey = ''; 
	  $this->alipayrsaPublicKey = ''; 
  }
  /*
  *该方法为支付宝转账方法
  *osn 交易单号
  *payee_account 收款人帐号
  *amount 转账金额
  *payee_real_name 收款方真实姓名
  */
  public function AlipayFundTransToaccountTransfer($osn,$payee_account,$amount,$payee_real_name){
	    $msg = array();
	    if(!$osn){
			$msg['success'] = 2;
			$msg['text']    = '交易单号为空';
			return $msg;
		}elseif(!$payee_account){
			$msg['success'] = 2;
			$msg['text']    = '收款人帐号为空';
			return $msg;
		}elseif(!$amount || $amount<0.1){
			$msg['success'] = 2;
			$msg['text']    = '转账金额不能小于0.1';
			return $msg;
		}elseif(!$payee_real_name){
			$msg['success'] = 2;
			$msg['text']    = '收款人姓名为空';
			return $msg;
		}else{
			$aop = new AopClient();
			$aop->gatewayUrl         = 'https://openapi.alipay.com/gateway.do';
			$aop->appId              = $this->appId;
			$aop->rsaPrivateKey      = $this->rsaPrivateKey;
			$aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
			$aop->apiVersion         = '1.0';
			$aop->signType           = 'RSA';
			$aop->postCharset        = 'UTF-8';
			$aop->format             = 'json';
			$request = new AlipayFundTransToaccountTransferRequest();
			$request->setBizContent("{" .
			"    \"out_biz_no\":\"".$osn."\"," .
			"    \"payee_type\":\"ALIPAY_LOGONID\"," .
			"    \"payee_account\":\"".$payee_account."\"," .
			"    \"amount\":\"".$amount."\"," .
			"    \"payer_real_name\":\"支付方名称\"," .
			"    \"payer_show_name\":\"支付方名称\"," .
			"    \"payee_real_name\":\"".$payee_real_name."\"," .
			"    \"remark\":\"转账备注\"," .
			"    \"ext_param\":\"{\\\"order_title\\\":\\\"用户提现\\\"}\"" .
			"  }");
			$result = $aop->execute ( $request); 
			$resultCode = $result->alipay_fund_trans_toaccount_transfer_response->code;
			$resultMsg  = $result->alipay_fund_trans_toaccount_transfer_response->sub_msg;
		    if(!empty($resultCode)&&$resultCode == 10000){
				$msg['success'] = 1;
				$msg['text']    = '支付宝转账成功';
				return $msg;
			}else{
				$msg['success'] = 2;
				$msg['text']    = $resultMsg;
				return $msg;
			}
		}
  }
}
?>